package musicstore.dao.impl;

import java.util.List;

import musicstore.dao.UserDao;
import musicstore.entity.User;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	public User fetchUser(String username, String password) {
		List<User> user = (List<User>) getHibernateTemplate().find("from User where username = ? and password = ?",username,password);
		if(user.size() == 0)
			return null;
		return user.get(0);
	}

	@Override
	public void add(User user) {
		getHibernateTemplate().save(user);
	}

	@Override
	public User fetchUserByUsername(String username) {
		String hql = "from User where username = ?";
		List<User> list = (List<User>)getHibernateTemplate().find(hql,username);
		return list.size()==0?null:list.get(0);
	}
}
